package com.yi.serviceprice.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yi.internalcommon.constant.CommonStatusEnum;
import com.yi.internalcommon.dto.PriceRule;
import com.yi.internalcommon.dto.ResponseResult;
import com.yi.serviceprice.mapper.PriceRuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author 田毅
 * @since 2022-12-21
 */
@Service
public class PriceRuleService {

    @Autowired
    private PriceRuleMapper priceRuleMapper;

    public ResponseResult add(PriceRule priceRule){
    //拼接fareType
        String vehicleType = priceRule.getVehicleType();
        String cityCode = priceRule.getCityCode();
        String fareType =cityCode +"$"+ vehicleType;
        priceRule.setFareType(fareType);
        //添加版本号
        LambdaQueryWrapper<PriceRule> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PriceRule::getCityCode,cityCode);
        lqw.eq(PriceRule::getVehicleType,vehicleType);
        lqw.orderByDesc(PriceRule::getFareVersion);
        List<PriceRule> priceRules = priceRuleMapper.selectList(lqw);
        Integer fareVersion = 0;
        if(priceRules.size()>0){
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EXISTS.getCode(),CommonStatusEnum.PRICE_RULE_EXISTS.getValue());
        }
        priceRule.setFareVersion(++fareVersion);
        priceRuleMapper.insert(priceRule);
        return ResponseResult.success("");
    }

    /**
     * 更新计价规则
     * @param priceRule
     * @return
     */
    public ResponseResult edit(PriceRule priceRule){
        //拼接fareType
        String vehicleType = priceRule.getVehicleType();
        String cityCode = priceRule.getCityCode();
        String fareType =cityCode +"$"+ vehicleType;
        priceRule.setFareType(fareType);
        //添加版本号
        LambdaQueryWrapper<PriceRule> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PriceRule::getCityCode,cityCode);
        lqw.eq(PriceRule::getVehicleType,vehicleType);
        lqw.orderByDesc(PriceRule::getFareVersion);
        List<PriceRule> priceRules = priceRuleMapper.selectList(lqw);
        Integer fareVersion = 0;
        if(priceRules.size()>0){
            PriceRule lasterPriceRule = priceRules.get(0);
            Double unitPricePerMile = lasterPriceRule.getUnitPricePerMile();
            Double unitPricePerMinute = lasterPriceRule.getUnitPricePerMinute();
            Double startFare = lasterPriceRule.getStartFare();
            Integer startMile = lasterPriceRule.getStartMile();
            if(unitPricePerMile.compareTo(priceRule.getUnitPricePerMile()) ==0
                    &&unitPricePerMinute.compareTo(priceRule.getUnitPricePerMinute())==0
            &&startFare.compareTo(priceRule.getStartFare())==0&&startMile.compareTo(priceRule.getStartMile())==0){
                return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_NOT_EDIT.getCode(),CommonStatusEnum.PRICE_RULE_NOT_EDIT.getValue());
            }
            fareVersion = lasterPriceRule.getFareVersion();
        }
        priceRule.setFareVersion(++fareVersion);
        priceRuleMapper.insert(priceRule);
        return ResponseResult.success("");
    }

    public ResponseResult getNewestVersion(String fareType) {
        LambdaQueryWrapper<PriceRule> lqw =new LambdaQueryWrapper();
        lqw.eq(PriceRule::getFareType,fareType);
        lqw.orderByDesc(PriceRule::getFareVersion);
        List<PriceRule> priceRules = priceRuleMapper.selectList(lqw);
        if (priceRules.size()>0){
            return ResponseResult.success(priceRules.get(0));
        }else {
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY.getCode(),CommonStatusEnum.PRICE_RULE_EMPTY.getValue());
        }
    }

    public ResponseResult<Boolean> isNew(String fareType, Integer fareVersion) {
        ResponseResult<PriceRule> newestVersion = getNewestVersion(fareType);
        if(newestVersion.getCode()==CommonStatusEnum.PRICE_RULE_EMPTY.getCode()){
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY.getCode(),CommonStatusEnum.PRICE_RULE_EMPTY.getValue());
//            return ResponseResult.success(false);
        }
        PriceRule priceRule =  newestVersion.getData();
        Integer fareVersionDB = priceRule.getFareVersion();
        if(fareVersionDB > fareVersion){
            return ResponseResult.success(false);
        }else {
            return ResponseResult.success(true);
        }
    }

    public ResponseResult<Boolean> ifExists(PriceRule priceRule){
        String cityCode = priceRule.getCityCode();
        String vehicleType = priceRule.getVehicleType();

        LambdaQueryWrapper<PriceRule> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PriceRule::getCityCode,cityCode);
        lqw.eq(PriceRule::getVehicleType,vehicleType);
        lqw.orderByDesc(PriceRule::getFareVersion);
        List<PriceRule> priceRules = priceRuleMapper.selectList(lqw);
        if(priceRules.size()>0){
            return ResponseResult.success(true);
        }else{
            return ResponseResult.success(false);
        }
    }
}
